<template>
    <div class="m-loginForm" :class="[animate.loginAnimate]" v-login-form="animate">
        <p class="title">欢迎使用配置中心</p>
        <p class="desc"><i class="el-icon-warning"></i>您做的每一个操作都非常重要，请您谨慎操作！</p>
        <el-form class="layout-1" ref="loginForm.data" :model="loginForm.data">
            <el-select v-model="checkedCompanyId" placeholder="请选择" class="companyList">
                <el-option v-for="company in companyList" :key="company.id" :label="company.name" :value="company.id">
                </el-option>
            </el-select>

            <div id="inputbox" >
                <div class="item">
                    <div class="icon icon-vip"></div>
                    <input type="text" v-model.trim="loginForm.data.userName" placeholder="请输入用户名">
                </div>
                <div class="item">
                    <div class="icon icon-locker"></div>
                    <input v-model.trim="loginForm.data.passWord" @keyup.enter="login" placeholder="登录密码" type="password" >                
                </div>
            </div>
            <div class="item-2">
                <div class="icon-error" v-show="loginForm.data.errorInfo"></div>
                <div class="words">{{loginForm.data.errorInfo}}</div>
            </div>
            <div class="item-3">
                <el-button type="primary" @click="login" class="login">登录</el-button>
            </div>
        </el-form>
    </div>
</template>
<script>
    import axios from "axios";
    import cgi from "conf/cgi";
    import runtime from "utils/runtime";
    import queryToJson from "lib/json/queryToJson";
    const companyList = [
        {
            name: "请选择公司",
            id: "0"
        }, {
            name: "金道环球投资",
            id: "fx"
        }, {
            name: "金道贵金属",
            id: "pm"
        }, {
            name: "恒信贵金属",
            id: "cf"
        }, {
            name: "创富金融",
            id: "hx"
        }
    ];
    export default {
        data(){
            return {
                companyList:companyList,
                checkedCompanyId:'0',
                animate:{
                    loginAnimate:'',
                },
                loginForm: {
                    data: {
                        userName: null,
                        passWord: null,
                        errorInfo: null,
                    }
                }
            }
        },
        methods: {
            login() {
                const that = this;
                if (!this.loginForm.data.userName) {
                    this.loginForm.data.errorInfo = '用户名不能为空';
                    this.animate.loginAnimate = 'animated shake';
                }
                else if (!this.loginForm.data.passWord) {
                    this.loginForm.data.errorInfo = '密码不能为空';
                    this.animate.loginAnimate = 'animated shake';
                }
                else {
                    this.loginForm.data.errorInfo = null;
                    return axios.post(cgi.loginForm, this.loginForm.data)
                        .then(res => {
                            if (res.code == 0) {
                                runtime.setUser(res.data.token);
                                loginSuccess(that);
                                return res.data;
                            } else {
                                return Promise.reject("请求数据失败!");
                            }
                        }, error => {
                            return Promise.reject(error.msg);
                        });

                }
            }
        },
        directives:{
            loginForm:{
                inserted:function(el,binding) {
                    const inputs = $(el).find('input');
                    inputs.on('focus',function(){
                        $(this).parents('.item').addClass('focused');
                    }).on('blur',function(){
                        $(this).parents('.item').removeClass('focused');
                    });

                    $("#inputbox").on('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend',function(){
                        if( $(this).hasClass('shake') ) binding.value.loginAnimate='';
                    });
                }
            }
        }
    }
    
    //登录成功调用
    function loginSuccess(that){
        new Promise(function(resolve,reject){
            that.animate.loginAnimate = 'animated zoomOut';
            setTimeout(function(){
                resolve();
            },300);
        }).then(function(){
            let queryObject = queryToJson(location.search.slice(1));
            if (queryObject.form) {
                location.href = queryObject.form;
            } else {
                location.href = '/appSetup.html';
            }
        });
    }
</script>
<style lang="scss" type="text/scss">
    .m-loginForm {
        display:block;
        width:400px;
        margin: 150px auto 0;
        
        .title{
            text-align:center;
            font-size: 38px;
            color: #333;
            margin: 0 0 20px;
        }
        .desc{
            margin: 20px 0;
            border: 1px solid rgba(247, 186, 42, 0.58);
            background: rgba(247, 186, 42, 0.12);
            padding: 8px;
            font-size: 14px;
            border-radius: 4px;
            color: #e8a403;

            i{
                margin-right: .5em;
                color: #F7BA2A;
            }
        }
        .item {
            position: relative;
            box-sizing: border-box;
            padding: 5px 15px;
            border: solid 1px #dbd5d5;
            border-radius: 5px;
            margin-top: 20px;
            transition:all .3s linear;
            .icon {
                position: absolute;
                z-index: 10;
                margin-top: 5px;
            }
            input {
                border: none;
                padding-left: 40px;
                height: 38px;
                outline: none;
            }
            &::after{
                content: '';
                position: absolute;
                left: 50%;
                bottom: 0;
                height: 1px;
                width: 0;
                overflow: hidden;
                transform: translate(-50%, 0);
                background: #20A0FF;
                border-radius: 5px;
                transition:all .3s linear;
            }
            &.focused{
                border-color:#20A0FF;
                &::after{ width: 100%; }
            }
        }
        .vcode {
            height: 48px;
            padding: 5px;
            img {
                width: 100%;
                height: 100%;
            }
        }
        .item-2 {
            font-size: 14px;
            line-height: 31px;
            vertical-align: middle;
            color: #FF4949;
            div {
                display: inline-block;
            }
            .words {
                position: relative;
                top: -3px;
                height: 17px;
                line-height: 17px;
                margin-left: 5px;
            }
        }
        .item-3 {
            .login {
                width: 100%;
                //line-height: 60px;
                font-size: 19px;
            }
        }
        .companyList{
            width: 100%;
            
            .el-input__inner{
                height: 45px;
            }
        }
        .animated{
            animation-fill-mode: forwards;
        }
    }
</style>